#include <iostream>
#include <gtest/gtest.h>

using namespace std;

int a = 0;
class MyEnvironment : public testing::Environment // 全局测试套件
{
public:
    virtual void SetUp() override
    {
        cout << "所有单元测试前执行环境初始化" << endl;
        a = 10;
    }
    virtual void TearDown() override
    {
        cout << "所有单元测试后执行环境清理" << endl;
        a = 0;
    }
};

TEST(MyEnvironment, test1) // 测试套件名称：单元测试名称
{
    cout << "单元测试1" << endl;
    EXPECT_EQ(a, 10);
    a = 20;
}

TEST(MyEnvironment, test2)
{
    cout << "单元测试2" << endl;
    ASSERT_EQ(a, 10); // 全局测试套件中的单元测试会互相影响
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MyEnvironment); // 添加全局测试套件
    // 可以添加多个全局测试套件，每个全局测试套件都有一组单元测试，套件之间互不影响
    RUN_ALL_TESTS();
    return 0;
}